home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / startup / startup.s < prev   
Encoding:
Text File  |  1998-10-04  |  11.7 KB  |  619 lines

  1. ;
  2. ; startup.lib (linked library)
  3. ;
  4. ; startup and library management code for all ACE programs.
  5. ; Copyright (C) 1998 David Benn
  6. ; This program is free software; you can redistribute it and/or
  7. ; modify it under the terms of the GNU General Public License
  8. ; as published by the Free Software Foundation; either version 2
  9. ; of the License, or (at your option) any later version.
  10. ;
  11. ; This program is distributed in the hope that it will be useful,
  12. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ; GNU General Public License for more details.
  15. ;
  16. ; You should have received a copy of the GNU General Public License
  17. ; along with this program; if not, write to the Free Software
  18. ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. ;
  20. ; Author: David J Benn
  21. ;   Date: 3rd,17th,24th,28th,30th November 1991,
  22. ;      27th January 1992, 
  23. ;       2nd,4th,11th,14th-16th,29th February 1992,
  24. ;      1st March 1992,
  25. ;      26th April 1992,
  26. ;      1st,2nd,11th,12th,15th May 1992,
  27. ;      6th,7th,11th,28th,30th June 1992,
  28. ;      4th,7th,16th July 1992
  29. ;      5th,7th,8th,22nd December 1992,
  30. ;      6th,11th,28th February 1993,
  31. ;      1st March 1993,
  32. ;      6th June 1993,
  33. ;      9th,25th October 1993,
  34. ;      27th December 1993,
  35. ;      5th November 1995
  36. ;
  37. ; - Some routines in startup.lib open the shared libaries required by 
  38. ;   an ACE program along with retrieving DOS and Intuition data.
  39. ; - startup.lib also contains startup code for Workbench and Shell launched
  40. ;   programs.
  41. ; - If a library can't be opened or data can't be accessed, _starterr is set
  42. ;   to 1 so that upon return to the main ACE program an abort branch can be 
  43. ;   executed.
  44. ; - If _stdin or _stdout are zero, a WorkBench start is assumed.
  45. ;
  46.  
  47. ; * CONSTANTS *
  48. pr_CLI         equ     172
  49. pr_MsgPort     equ     92
  50. sm_ArgList    equ    36
  51. wa_Lock        equ    0
  52.  
  53.     ; * XDEFs *
  54.     xdef    _DOSBase
  55.     xdef    _MathBase
  56.         xdef    _MathTransBase
  57.         xdef    _MathIeeeSingTransBase
  58.     xdef    _GfxBase
  59.     xdef    _IntuitionBase
  60.     xdef    _TransBase
  61.     xdef    _SysBase
  62.  
  63.     xdef    _RPort
  64.     xdef    _Wdw
  65.     xdef    _Scrn
  66.     xdef    _ViewPort
  67.     xdef    _Wdw_width
  68.     xdef    _Wdw_height
  69.     xdef    _Wdw_id
  70.     xdef    _WBWdw
  71.     xdef    _WBRPort
  72.     xdef    _WBViewPort
  73.     xdef    _WBScrn
  74.     xdef    _WBbgpen
  75.     xdef    _WBfgdpen
  76.     xdef    _IntuiMode
  77.  
  78.     xdef    _returncode
  79.  
  80.     xdef    _cursorON
  81.     xdef    _cursorOFF
  82.     xdef    _turncursoron
  83.     xdef    _turncursoroff
  84.  
  85.     xdef    _cmdlen
  86.     xdef    _cmdbuf
  87.     xdef    _WBenchMsg
  88.     xdef    _TaskAddress
  89.     xdef    _startup
  90.     xdef    _cleanup
  91.     xdef    _starterr
  92.     xdef    _stdout
  93.     xdef    _stdin
  94.     xdef    _openio
  95.     xdef    _closeio
  96.     xdef    _openmathffp
  97.     xdef    _closemathffp
  98.     xdef    _openmathtrans
  99.     xdef    _closemathtrans
  100.     xdef    _opengfx
  101.     xdef    _closegfx
  102.     xdef    _openintuition
  103.     xdef    _closeintuition
  104.     xdef    _opentranslator
  105.     xdef    _closetranslator
  106.     xdef    _fgdpen
  107.     xdef    _bgpen
  108.  
  109.     ; * XREFs needed by startup.lib *
  110.     xref    _AbsExecBase
  111.     xref    _LVOOpenLibrary
  112.     xref    _LVOCloseLibrary
  113.     xref    _LVOFindTask
  114.     xref    _LVOWaitPort
  115.     xref    _LVOGetMsg
  116.     xref    _LVOReplyMsg
  117.     xref    _LVOForbid
  118.     xref    _LVOFreeMem
  119.  
  120.     xref    _LVOOutput
  121.     xref    _LVOInput
  122.            xref    _LVOWrite
  123.     xref    _LVOCurrentDir
  124.  
  125.     xref    _RPort_list
  126.     xref    _Wdw_list
  127.     xref    _Wdw_width_list
  128.     xref    _Wdw_height_list
  129.     xref    _stdout_list
  130.     xref    _stdin_list
  131.     xref    _fgdpen_list
  132.     xref    _bgpen_list
  133.     xref    _storewindowinfo
  134.     xref    _tg_degs
  135.     xref    _tg_tx
  136.     xref    _tg_ty
  137.     xref    _tg_initx
  138.     xref    _tg_inity
  139.     xref    _tg_pen
  140.     xref    _LVOMove
  141.     xref    _turncursoron
  142.     xref    _turncursoroff
  143.  
  144.     xref    _argbuf
  145.  
  146.     xref    _initialise_waveform_data
  147.     xref    _waveformaddr0
  148.     xref    _waveformaddr1
  149.     xref    _waveformaddr2
  150.     xref    _waveformaddr3
  151.     xref    _wavelen0
  152.     xref    _wavelen1
  153.     xref    _wavelen2
  154.     xref    _wavelen3
  155.     xref    _sinwavedata
  156.  
  157. ;
  158. ; opens the DOS library, gets stdio handles & sets up default sound waveform.
  159. ;
  160. _openio:
  161.     move.b    #0,_starterr
  162.             
  163.     ; open DOS library
  164.     movea.l    #_doslib,a1
  165.     move.l    #0,d0            ; version = don't care
  166.     movea.l _AbsExecBase,a6
  167.     jsr    _LVOOpenLibrary(a6)
  168.     move.l    d0,_DOSBase
  169.     cmpi.l    #0,d0
  170.     bne.s    _openio_ok
  171.     move.b    #1,_starterr        ; error
  172.     rts
  173.  
  174. _openio_ok:    
  175.     ; get _stdout handle
  176.     move.l    _DOSBase,a6        
  177.     jsr    _LVOOutput(a6)
  178.     move.l    d0,_stdout
  179.     
  180.     ; get _stdin handle
  181.     move.l    _DOSBase,a6
  182.     jsr    _LVOInput(a6)
  183.     move.l    d0,_stdin
  184.     
  185. _stdin_ok:
  186.       cmpi.l    #0,d0
  187.     beq.s    _soundsetup        ; no I/O handles, no cursor changes!
  188.  
  189.     jsr    _turncursoroff
  190.      
  191. _soundsetup:
  192.         ; set up default waveform data for SOUND statement.
  193.     jsr    _initialise_waveform_data
  194.         
  195. _quitopenio:
  196.     rts
  197.  
  198. ;
  199. ; closes DOS library.
  200. ;
  201. _closeio:
  202.     tst.l    _DOSBase
  203.     beq.s    _quitcloseio
  204.     
  205.     move.l    _DOSBase,a1
  206.     movea.l    _AbsExecBase,a6
  207.     jsr    _LVOCloseLibrary(a6)
  208.  
  209.     cmpi.l    #0,_stdout
  210.     beq.s    _quitcloseio        ; don't turn cursor on if no stdout!
  211.     
  212.     jsr    _turncursoron
  213.  
  214. _quitcloseio:
  215.     rts
  216.  
  217. ;
  218. ; opens Motorola Fast-Floating-Point (FFP) math library.
  219. ;
  220. _openmathffp:
  221.     move.b    #0,_starterr
  222.  
  223.     movea.l #_mathffplib,a1
  224.     move.l  #0,d0
  225.     movea.l _AbsExecBase,a6
  226.     jsr     _LVOOpenLibrary(a6)
  227.     move.l  d0,_MathBase
  228.     cmpi.l    #0,d0
  229.     bne.s    _mathffp_ok
  230.     move.b    #1,_starterr
  231.  
  232. _mathffp_ok:
  233.         rts
  234.  
  235. ;
  236. ; closes FFP library.
  237. ;
  238. _closemathffp:
  239.     tst.l    _MathBase
  240.     beq.s    _quitclosemathffp
  241.  
  242.     move.l  _MathBase,a1
  243.     movea.l _AbsExecBase,a6
  244.     jsr     _LVOCloseLibrary(a6)
  245.  
  246. _quitclosemathffp:
  247.     rts
  248.        
  249. ;
  250. ; opens FFP and IEEE transendental function libraries.
  251. ;
  252. _openmathtrans:
  253.     move.b    #0,_starterr
  254.     
  255.     movea.l #_mathtranslib,a1
  256.     move.l  #0,d0
  257.     movea.l _AbsExecBase,a6
  258.     jsr     _LVOOpenLibrary(a6)
  259.     move.l  d0,_MathTransBase
  260.     cmpi.l    #0,d0
  261.     bne.s    _openieeesingtranslib
  262.     move.b    #1,_starterr
  263.     rts            ; if we can't open FFP library -> abort!
  264.  
  265. _openieeesingtranslib:
  266.     ;
  267.     ; IEEE SP Transcendental library is currently only
  268.     ; used by the _power function. If it can't be opened,
  269.     ; FFP exponentiation function is used instead, so don't
  270.     ; set _starterr if we can't open mathieeesingtrans.library.
  271.     ; See ACE/src/lib/c/pow.c for more details.
  272.     ; 
  273.     movea.l #_mathieeesingtranslib,a1
  274.     move.l  #0,d0
  275.     movea.l _AbsExecBase,a6
  276.     jsr     _LVOOpenLibrary(a6)
  277.     move.l  d0,_MathIeeeSingTransBase    
  278.  
  279.         rts
  280.  
  281. ;
  282. ; close FFP transendental function library.
  283. ;
  284. _closemathtrans:
  285.     tst.l    _MathTransBase
  286.     beq.s    _quitclosemathtrans
  287.  
  288.     move.l  _MathTransBase,a1
  289.     movea.l _AbsExecBase,a6
  290.     jsr     _LVOCloseLibrary(a6)
  291.  
  292. _quitclosemathtrans:
  293.     rts
  294.  
  295. ;
  296. ; open intuition library and get standard rastport.
  297. ;
  298. _openintuition:
  299.     move.b    #0,_starterr
  300.  
  301.     ; open library
  302.     movea.l    #_intuitionlib,a1
  303.     move.l    #0,d0
  304.     movea.l    _AbsExecBase,a6
  305.     jsr    _LVOOpenLibrary(a6)
  306.     move.l    d0,_IntuitionBase
  307.     cmpi.l    #0,d0
  308.     bne.s    _intuition_ok
  309.     move.b    #1,_starterr        ; error
  310.     rts
  311.  
  312. _intuition_ok:
  313.     ; get window, rastport
  314.     ; and viewport
  315.     movea.l    d0,a1
  316.     movea.l    52(a1),a1        ; ActiveWindow 
  317.     move.l    a1,_Wdw
  318.     cmpa.l    #0,a1
  319.     bne.s    _Wdw_ok
  320.     move.b    #1,_starterr        ; error (no startup window -> shell/cli)
  321.     rts
  322.  
  323. _Wdw_ok:
  324.     move.l    50(a1),_RPort        ; Rastport of ActiveWindow
  325.     
  326.     ; approx. size of window (Wb 2.04 shell)
  327.     move.w    #640,_Wdw_width
  328.     move.w    #100,_Wdw_height
  329.  
  330.     ; get Wb screen
  331.     movea.l    _IntuitionBase,a1
  332.     movea.l    56(a1),a1        
  333.     move.l    a1,_Scrn        ; ActiveScreen
  334.     move.l    a1,d0
  335.     add.l    #44,d0
  336.     move.l    d0,_ViewPort        ; ViewPort is 44 bytes 
  337.                     ; from start of screen structure.
  338.  
  339.     ; store Wb globals
  340.     move.l    _RPort,_WBRPort
  341.     move.l    _ViewPort,_WBViewPort
  342.     move.l    _Scrn,_WBScrn
  343.     move.l    _Wdw,_WBWdw
  344.     move.b    #0,_IntuiMode
  345.  
  346.     ; number of Wb window (shell/cli)
  347.     move.w    #0,_Wdw_id
  348.  
  349.     move.w    #1,_fgdpen        ; current drawing pen color
  350.     move.w    #0,_bgpen        ; current background pen color
  351.     move.w    #1,_WBfgdpen        
  352.     move.w    #0,_WBbgpen        
  353.  
  354.     ; in case _heading ONLY is 
  355.     ; called without any TG stuff!
  356.  
  357.     move.w    #270,_tg_degs        ; initial heading is 270 degs (north)
  358.  
  359.     rts
  360.  
  361. ;
  362. ; close intuition library.
  363. ;
  364. _closeintuition:
  365.     tst.l    _IntuitionBase
  366.     beq.s    _quitcloseintuition
  367.  
  368.     move.l  _IntuitionBase,a1
  369.     movea.l _AbsExecBase,a6
  370.     jsr     _LVOCloseLibrary(a6)
  371.  
  372. _quitcloseintuition:
  373.     rts
  374.  
  375. ;
  376. ; open graphics library.
  377. ;
  378. _opengfx:
  379.     move.b    #0,_starterr
  380.  
  381.     ; open library
  382.     movea.l    #_gfxlib,a1
  383.     move.l    #0,d0
  384.     movea.l    _AbsExecBase,a6
  385.     jsr    _LVOOpenLibrary(a6)
  386.     move.l    d0,_GfxBase
  387.     cmpi.l    #0,d0
  388.     bne.s    _gfx_ok
  389.     move.b    #1,_starterr        ; error
  390.  
  391. _gfx_ok:
  392.     ; initialise turtle graphics 
  393.     move.w    #270,_tg_degs        ; initial heading is 270 degs (north)
  394.     move.w    _tg_initx,_tg_tx    ; initial x coordinate
  395.     move.w    _tg_inity,_tg_ty    ; initial y coordinate
  396.     move.l    _GfxBase,a6
  397.     move.l    _RPort,a1
  398.     move.w    _tg_tx,d0
  399.     move.w    _tg_ty,d1
  400.     jsr    _LVOMove(a6)        ; move to home position 
  401.     move.b    #1,_tg_pen        ; pen is initially down
  402.  
  403.     rts
  404.  
  405. ;
  406. ; close graphics library.
  407. ;
  408. _closegfx:
  409.     tst.l    _GfxBase
  410.     beq.s    _quitclosegfx
  411.  
  412.     move.l  _GfxBase,a1
  413.     movea.l _AbsExecBase,a6
  414.     jsr     _LVOCloseLibrary(a6)
  415.  
  416. _quitclosegfx:
  417.     rts
  418.  
  419. ;
  420. ; open translator library
  421. ;
  422. _opentranslator:
  423.     move.b    #0,_starterr
  424.  
  425.     movea.l    #_translatorlib,a1
  426.     move.l    #0,d0
  427.     movea.l    _AbsExecBase,a6
  428.     jsr    _LVOOpenLibrary(a6)
  429.     move.l    d0,_TransBase
  430.     cmpi.l    #0,d0
  431.     bne.s    _translator_ok
  432.     move.b    #1,_starterr        ; error
  433.  
  434. _translator_ok:
  435.     rts
  436.  
  437. ;
  438. ; close translator library
  439. ;
  440. _closetranslator:
  441.     tst.l    _TransBase
  442.     beq.s    _quitclosetranslator
  443.     
  444.     move.l  _TransBase,a1
  445.     movea.l _AbsExecBase,a6
  446.     jsr     _LVOCloseLibrary(a6)
  447.  
  448. _quitclosetranslator:
  449.     rts
  450.  
  451. ;
  452. ; Startup code for all ACE programs. 
  453. ;
  454. _startup:
  455.     ; save d0 & a0 in case CLI
  456.     ; arg's required.
  457.     move.l    d0,_cmdlen
  458.     move.l    a0,_cmdbuf
  459.  
  460.     ; make _SysBase available to ami.lib
  461.     move.l    _AbsExecBase,_SysBase
  462.  
  463.     ; open dos.library
  464.     jsr    _openio
  465.     tst.l    _DOSBase
  466.     beq.s    _quitstartup    ; _starterr will be set to 1
  467.  
  468.     ; get task's address
  469.     movea.l    _AbsExecBase,a6
  470.     suba.l    a1,a1
  471.     jsr    _LVOFindTask(a6)
  472.     move.l    d0,_TaskAddress
  473.     
  474.     ; look at Process structure
  475.     ; to determine whether
  476.     ; process started from Wb
  477.     movea.l    d0,a4
  478.     tst.l    pr_CLI(a4)        
  479.     beq.s    _fromWb
  480.     move.l    #0,_WBenchMsg     ; no message
  481.     rts             ; Not started from Wb
  482.  
  483. _fromWb:
  484.     ; wait for Wb message
  485.     movea.l    _AbsExecBase,a6
  486.     lea    pr_MsgPort(a4),a0
  487.     jsr    _LVOWaitPort(a6) ; wait for msg
  488.     lea    pr_MsgPort(a4),a0
  489.     jsr    _LVOGetMsg(a6)     ; get msg
  490.     move.l    d0,_WBenchMsg     ; store msg    
  491.  
  492.     ; make current directory
  493.     ; the working directory.
  494.     movea.l    d0,a2
  495.     move.l    sm_ArgList(a2),d0
  496.     beq.s    _quitstartup
  497.     movea.l    _DOSBase,a6
  498.     movea.l    d0,a0
  499.     move.l    wa_Lock(a0),d1        
  500.     jsr    _LVOCurrentDir(a6)
  501.     
  502. _quitstartup:
  503.     rts    
  504.  
  505. ;
  506. ; Cleanup code for all ACE programs.
  507. ;
  508. _cleanup:
  509.     ; free allocated argument buffer. 
  510.     tst.l    _argbuf
  511.     beq.s    _closedos    ; if no memory allocated -> skip freemem
  512.     movea.l    _AbsExecBase,a6
  513.     movea.l    _argbuf,a1    ; buffer address
  514.     move.l    _cmdlen,d0    ; # of bytes allocated (see arg.c)
  515.     jsr    _LVOFreeMem(a6)
  516.     
  517. _closedos:
  518.     ; close dos.library
  519.     jsr    _closeio    
  520.  
  521.     ; if there is a Wb message, reply to it.
  522.     tst.l    _WBenchMsg
  523.     bne.s    _replytoWBMsg    
  524.  
  525.     ; otherwise just exit with return code (CLI/shell start).
  526.     move.l    _returncode,d0
  527.     rts
  528.  
  529. _replytoWBMsg:
  530.     ; exit from a Wb launched program. 
  531.     move.l    _AbsExecBase,a6
  532.     jsr    _LVOForbid(a6)
  533.     move.l    _WBenchMsg,a1
  534.     jsr    _LVOReplyMsg(a6)
  535.     rts
  536.  
  537. ;
  538. ; turn cursor on.
  539. ;
  540. _turncursoron:
  541.     move.l    _stdout,d1
  542.     move.l    #_cursorON,d2
  543.     moveq    #3,d3
  544.     move.l    _DOSBase,a6
  545.     jsr    _LVOWrite(a6)
  546.     rts
  547.  
  548. ;
  549. ; turn cursor off.
  550. ;
  551. _turncursoroff:
  552.     move.l    _stdout,d1
  553.     move.l    #_cursorOFF,d2
  554.     moveq    #4,d3
  555.     move.l    _DOSBase,a6
  556.     jsr    _LVOWrite(a6)
  557.     rts
  558.  
  559. ;************************
  560.  
  561.     SECTION    startup_data,DATA
  562.  
  563. _version:    dc.b    '$VER: startup 1.12 (05.11.95)',0
  564.  
  565. _doslib:        dc.b    'dos.library',0
  566. _mathffplib:        dc.b    'mathffp.library',0
  567. _mathtranslib:      dc.b    'mathtrans.library',0
  568. _mathieeesingtranslib:     dc.b    'mathieeesingtrans.library',0
  569. _gfxlib:        dc.b    'graphics.library',0
  570. _intuitionlib:        dc.b    'intuition.library',0
  571. _translatorlib:     dc.b    'translator.library',0
  572.  
  573. _IntuiMode:    dc.b     0
  574.  
  575. _returncode:    dc.l    0
  576.  
  577. _cursorOFF:    dc.b    $9b,$30,$20,$70
  578. _cursorON:    dc.b    $9b,$20,$70
  579.  
  580.     SECTION startup_mem,BSS
  581.  
  582. _cmdlen:    ds.l 1
  583. _cmdbuf:    ds.l 1
  584. _starterr:    ds.b 1
  585. _DOSBase:    ds.l 1
  586. _stdout:    ds.l 1
  587. _stdin:        ds.l 1
  588.  
  589. _MathBase:          ds.l 1
  590. _MathTransBase:     ds.l 1
  591. _MathIeeeSingTransBase: ds.l 1
  592. _GfxBase:        ds.l 1
  593. _IntuitionBase:     ds.l 1
  594. _TransBase:        ds.l 1
  595. _SysBase:        ds.l 1
  596.  
  597. _RPort:        ds.l 1
  598. _Wdw:        ds.l 1
  599. _Scrn:        ds.l 1
  600. _ViewPort:    ds.l 1
  601. _Wdw_width:    ds.w 1
  602. _Wdw_height:    ds.w 1
  603. _Wdw_id:    ds.w 1
  604. _WBWdw:        ds.l 1
  605. _WBRPort:    ds.l 1
  606. _WBViewPort:    ds.l 1
  607. _WBScrn:    ds.l 1
  608. _WBbgpen:    ds.w 1
  609. _WBfgdpen:    ds.w 1
  610.  
  611. _fgdpen:    ds.w 1
  612. _bgpen:        ds.w 1
  613.  
  614. _TaskAddress:    ds.l 1
  615. _WBenchMsg:    ds.l 1
  616.  
  617.     END
  618.